package com.xianbeiying.app.ckgl.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.xianbeiying.app.ckgl.R;

/**
 * Created by yangjie on 16/10/16.
 */
public class MyDialog extends Dialog{
    private Context context;
    private int themeResId;
    public MyDialog(Context context, int themeResId){
        super(context, themeResId);
        this.context=context;
        this.themeResId=themeResId;

    }
    public void showView(){
        View view = ViewUtils.getLayout(context,
                R.layout.dialog_ly);
        ImageView img = (ImageView) view.findViewById(R.id.jiazai1);
        Animation anim = AnimationUtils.loadAnimation(context,
                R.anim.dailog);
        anim.setInterpolator(new LinearInterpolator());//不停顿
        img.startAnimation(anim);
        initView(context, themeResId, 0, view,
                Gravity.CENTER, true);
    }

    private void initView(Context context, int dialogStyle,
                          int dialogAnimation, View view, int gravity, boolean isBackCannel) {
        this.setContentView(view);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        Window window =getWindow();
        window.setAttributes(lp);
        window.setGravity(gravity);
        if (dialogAnimation > 0) {
            window.setWindowAnimations(dialogAnimation);
        }
        if (!(context instanceof Activity)) {
            window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
            window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        show();
        if (isBackCannel) {
            //关闭
            setCancelable(true);
            setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface arg0, int arg1,
                                     KeyEvent arg2) {
                    if (arg2.getAction() == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
    }

    public  void closeDialog(){
        if (this!= null) {
            this.dismiss();
        }
    }
}
